var ACTION = { "ADD": 1, "DELETE": 2, "UPDATE": 3 };

function btnupdate(tb, size, element){
	if(window["etat" + tb].length > 0){
		window["etat" + tb] = [];
		addDelMod(tb, ACTION.UPDATE, getValues(tb, size, element, ACTION.UPDATE));
	}
	else{
		if(!document.getElementById("txtupdate1")){
			var n = 1;
			window["etat" + tb] = [];
			for (var i = 0;i<size;i++){
				window["etat" + tb][i] = $("#" + tb +" tr:nth-child(" + element + ") td:nth-child(" + (i + 1) + ")").html();
				if($.isArray(window["def" + tb][i])){
					var text = $("#" + tb +" tr:nth-child(" + element + ") td:nth-child(" + (i + 1) + ")").html();
					var choix = "<select id='txtupdate" + (i+1) + "'>";
					for(var a = 0;a < window["def" + tb][i].length;a++){
						choix += "<option value='" + window["def" + tb][i][a] + (window["def" + tb][i][a] == text ? "' selected>" : "'>") + window["def" + tb][i][a] + "</option>";
					}
					choix += "</select>";
					$(document).ready(function(){
						$("#" + tb +" tr:nth-child(" + element + ") td:nth-child(" + (i + 1) + ")").html(choix);
					});
				}
				else if(window["def" + tb][i] == "txt"){
					var text = $("#" + tb +" tr:nth-child(" + element + ") td:nth-child(" + (i + 1) + ")").html();
					$(document).ready(function(){
						$("#" + tb +" tr:nth-child(" + element + ") td:nth-child(" + (i + 1) + ")").html("<input type='text' id='txtupdate" + n + "' value='" + text + "'/>");
					});
				}
				n++;
			}
			$("#delete" + tb + element).val("Cancel");
		}
	}
}
function btndelete(tb, size, element){
	if(window["etat" + tb].length > 0){
		for (var i=0;i<size + 1;i++){
			$("#" + tb +" tr:nth-child(" + element + ") td:nth-child( " + (i + 1) + ")").html(window["etat" + tb][i]);
		}
		window["etat" + tb] = [];
		$("#delete" + tb + element).val("Delete");
	}
	else{
		window["etat" + tb] = [];
		addDelMod(tb, ACTION.DELETE, getValues(tb, size, element, ACTION.DELETE));
	}
}
function btnadd(tb,size){
	var temp = [];
	for (var i=0;i<size;i++){
		temp[i] = $("#txtaddtblien" + (i + 1)).val();
	}
	addDelMod(tb, ACTION.ADD, getValues(tb, size, temp, ACTION.ADD));
}
function getValues(tb, size, index, act){
	var rep = [];
	var n = 0;
	for(var a = 0;a<size;a++){
		if(window["def" + tb][a] != "btn"){
			if(act == ACTION.ADD){
				rep[n] = $("#txtadd" + tb + (n + 1)).val();
			}
			else if(act == ACTION.DELETE){
				rep[n] = $("#" + tb +" tr:nth-child(" + index + ") td:nth-child(" + (a + 1) + ")").html();
			}
			else{
				rep[n] = $("#txtupdate" + (n + 1)).val();
			}
		}
		n++;
	}
	return rep;
}
function getValue(txt){
   return txt.substr(txt.search("value=") + 7, txt.indexOf(">") - txt.search("value=") - 8);
}